package com.qf.mybatisplus.utils;


import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class PdfUtils {

    private Map<String, String> map;

    // 利用模板生成pdf
    public static void pdfout(Map<String,Object> o) throws IOException {

        Date date = DateUtils.getCurrDate();
        String format = DateUtils.format(date, "yyyyMMddhhmmss");
        // 模板路径
        String templatePath = "D:/moko/background/testpdf/存量房网签合同(模板).pdf";
        // 生成的新文件路径
        String newPDFPath = "D:/moko/background/resultPDF/testout"+format+".pdf";

        String hth = DateUtils.getYear()+(int)(Math.random() * 1000000);
        String diskPath = "D:/moko/background/resultPDF/";
        //增加条形码
        TXMUtils.creatCode128(diskPath+hth+".png",hth);

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font FontChinese = new Font(bf, 10, Font.NORMAL);
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //文字类的内容处理
            Map<String,String> datemap = (Map<String,String>)o.get("datemap");
            form.addSubstitutionFont(bf);
            for(String key : datemap.keySet()){
                String value = datemap.get(key);
                form.setField(key,value);
            }

            Map<String,String> imgmap = new HashMap();
            imgmap.put("txm",diskPath+hth+".png");
//            图片类的内容处理
//            Map<String,String> imgmap = (Map<String,String>)o.get("imgmap");
            for(String key : imgmap.keySet()) {
                String value = imgmap.get(key);
                String imgpath = value;
                int pageNo = form.getFieldPositions(key).get(0).page;
                Rectangle signRect = form.getFieldPositions(key).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                //根据路径读取图片
                Image image = Image.getInstance(imgpath);
                //获取图片页面
                PdfContentByte under = stamper.getOverContent(pageNo);
                //图片大小自适应
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                //添加图片
                image.setAbsolutePosition(x, y);
                under.addImage(image);
            }
            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            Font font = new Font(bf, 32);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage1 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            PdfImportedPage importPage2 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 2);
            PdfImportedPage importPage3 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 3);
            copy.addPage(importPage1);
            copy.addPage(importPage2);
            copy.addPage(importPage3);
            doc.close();

        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }

    }

    public static void main(String[] args) throws IOException {
        Date date = DateUtils.getCurrDate();
        String format = DateUtils.format(date, "yyyyMMddhhmmss");
        // 模板路径
        String templatePath = "D:/moko/nginx-1.20.1/html/MOKO-IMG/testpdf/存量房网签合同(测试数据).pdf";
        // 生成的新文件路径
        String newPDFPath = "D:/moko/nginx-1.20.1/html/MOKO-IMG/resultPDF/邵阳网签合同/testout存量房网签合同(测试数据)"+".pdf";

        String hth = DateUtils.getYear()+(int)(Math.random() * 1000000);
        String testPath = "D:/moko/nginx-1.20.1/html/MOKO-IMG/resultPDF/2021686139.png";
        String diskPath = "D:/moko/nginx-1.20.1/html/MOKO-IMG/resultPDF/邵阳网签合同/";
        //增加条形码
        TXMUtils.creatCode128(diskPath+hth+".png",hth);

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font FontChinese = new Font(bf, 10, Font.NORMAL);
            // 输出流
            out = new FileOutputStream(newPDFPath);
            // 读取pdf模板
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();


        Map<String,String> map = new HashMap();
        map.put("htbh",hth);
        map.put("sellrymc","钟文辉");
        map.put("sellrytxdz","东台市文昌路1号");
        map.put("sellrysfzh","412725199805103698");
        map.put("buyrymc","张三、李四");
        map.put("buyrytxdz","东台市文昌路2号、东台市文昌路3号");
        map.put("buyrysfzh","412725199805103699、412725199805103610");
        map.put("zl","东台市文昌路1号3楼303");
        map.put("syqzh","99999999999");
        map.put("tdsyqzh","666666666");
        map.put("fwjg","钢混");
        map.put("fwyt","住宅");
        map.put("mj","94.31");
        map.put("zfdjrq","2021年10月1日");
        map.put("djjedx","壹佰万元整");
        map.put("cjje","1000000");
        map.put("cjjedx","贰佰万元整");
        map.put("zffs","分期付款");
        map.put("yjrq","2021-10-1");
        map.put("wydjfhr","30");
        map.put("znjzb","10");
        map.put("sfcdf","甲方");
        map.put("qtsx","无");
        map.put("sellrymc1","钟文辉");
        map.put("sellrylxfs1","1213639464");
        map.put("buyrymc1","张三");
        map.put("buyrylxfs1","12136394552");
        map.put("qdrq","2021年09月29日");
        map.put("sellrymc2","钟文辉");
        map.put("duihao1","√");
        map.put("duihao2","√");
        map.put("sellrymc3","钟文辉");
        map.put("sellrylxfs2","1213639464");
        map.put("qdrq1","2021年09月29日");
            //文字类的内容处理
            form.addSubstitutionFont(bf);
            for(String key : map.keySet()){
                String value = map.get(key);
                form.setField(key,value);
            }

            Map<String,String> imgmap = new HashMap();
            imgmap.put("txm",diskPath+hth+".png");
//            图片类的内容处理
//            Map<String,String> imgmap = (Map<String,String>)o.get("imgmap");
            for(String key : imgmap.keySet()) {
                String value = imgmap.get(key);
                String imgpath = value;
                int pageNo = form.getFieldPositions(key).get(0).page;
                Rectangle signRect = form.getFieldPositions(key).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                //根据路径读取图片
                Image image = Image.getInstance(imgpath);
                //获取图片页面
                PdfContentByte under = stamper.getOverContent(pageNo);
                //图片大小自适应
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                //添加图片
                image.setAbsolutePosition(x, y);
                under.addImage(image);
            }

            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            Font font = new Font(bf, 32);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            // 填充第一页
            PdfImportedPage importPage1 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            // 填充第二页
            PdfImportedPage importPage2 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 2);
            // 填充第三页
            PdfImportedPage importPage3 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 3);
            copy.addPage(importPage1);
            copy.addPage(importPage2);
            copy.addPage(importPage3);
            doc.close();

        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }
//
//        Map<String,String> map2 = new HashMap();
//        map2.put("img","c:/50336.jpg");

    }
}
